Encoder.EncodeType = 'numeric';

// Scroll to the given element
function snet_scroll_to(element)
{
	var offset = $(element).offset().top;
	$('html,body').animate({scrollTop: offset}, 1000);
}

function snet_change_form_status_type(type,num)
{
	if($('#snet_status_type_hidden').val() == num)
		return false;
		
	var old_type = $('#snet_status_type_hidden').val();
	var old_val = $('#snet_message_box_' + old_type).val();
	
	$('.snet_current_type').removeClass('snet_current_type');
	$('#snet_message_box_' + num).val(old_val);
	$('.snet_form_current_status_type').slideUp().removeClass('snet_form_current_status_type');
	$('#snet_status_update_form_icon_' + num).addClass('snet_current_type');
	$('#snet_status_update_' + type).slideDown().addClass('snet_form_current_status_type');
	$('#snet_status_type_hidden').val(num);
	
	// Wait. Let's share the link too, if switching from video to link or vice versa.
	if(num == 3)
	{
		var video_link = $('#snet_video_link').val();
		$('#snet_link').val(video_link);
	}
	else if(num == 5)
	{
		var link = $('#snet_link').val();
		$('#snet_video_link').val(link);
	}	
	
	return false;
}

function snet_change_photo_method(method)
{

	if($('#snet_photo_method_hidden').val() == method)
		return false;
		
	var old_method = $('#snet_photo_method_hidden').val();
	
	$('.button_strip_snet_photo_method' + old_method).removeClass('active');
	$('.button_strip_snet_photo_method' + method).addClass('active');
	$('#snet_photo_method_hidden').val(method);
	$('.snet_photo_method' + old_method).hide();
	$('.snet_photo_method' + method).show();
	
	return false;
}

function snet_delete_update(id,confirm_message)
{

	if(confirm(confirm_message) == true)
	{
		snet_ajax_indicator_show();

		// Delete it!
		$.ajax({
			url: smf_scripturl + '?action=snet-ajax;sa=deletestatus',
			global: false,
			type: 'GET',
			data: ({status : id}),
			success: function(dreturn){
				snet_ajax_indicator_hide();
				if(dreturn == 'true')
				{
					$('#status' + id).slideUp(400,function(){
						$(this).remove();
					});		
					$('#event' + id).slideUp(400,function(){
						$(this).remove();
					});						
				}
				else
					return true;
			},
			error: function(){
				snet_ajax_indicator_hide();
			}				
		}).responseText;
		return false;
	}

	return false;
}

function snet_delete_comment(id,status,confirm_message)
{

	if(confirm(confirm_message) == true)
	{
		snet_ajax_indicator_show();

		// Delete it!
		$.ajax({
			url: smf_scripturl + '?action=snet-ajax;sa=deletecomment',
			global: false,
			type: 'GET',
			data: ({comment : id}),
			success: function(dreturn){
				snet_ajax_indicator_hide();
				if(dreturn == 'true')
				{
					$('#comment' + id).slideUp(400,function(){
						$(this).remove();
					});
					$('#snet_num_comments_' + status).html(parseInt($('#snet_num_comments_' + status).html())-1);
					
					if($('#comments' + status).html() == '')
						$('#comments' + status).hide();
				}
				else
					return true;
			},
			error: function(){
				snet_ajax_indicator_hide();
			}				
		}).responseText;
		return false;
	}

	return false;
}

function snet_post_comment(status,profile)
{
	var snet_comment = Encoder.htmlEncode($('#snet_comment_field_' + status).val());
	var snet_convo_mode = parseInt($('#snet_enable_convo_mode').val()); // Conversation mode is nice. But do we use it?
	
	// Go ahead and contact the server if it ain't empty.
	if(snet_comment != '')
	{
		snet_ajax_indicator_show();
		$.ajax({
			url: smf_scripturl + '?action=snet-ajax;sa=postcomment',
			global: false,
			type: 'POST',
			data: ({snet_comment : snet_comment, status : status, profile : profile}),
			success: function(creturn){
				snet_ajax_indicator_hide();
				
				// Is it an error message? Oh my, oh my.
				if((strpos(creturn,'snet_error_') !== false))
				{
					creturn = creturn.replace('snet_error_','');
					snet_comment_error(creturn,status);
				}
				else
				{
					// It's posted. Display it to the user so the lazy peep doesn't have to reload the window.
					$('#comments' + status).append(creturn).show();
					$('.snet_date').each(function(){
						var date = $(this).attr('id');
						var date = parseInt(date.replace('d_','')) * 1000;
						var date = new Date(date);
						var last_date = new Date()-3;
						if( date < last_date )
						$(this).countdown({since: date, significant: 1, layout: '{d<}{dn} days {d>}{h<}{hn} hours {h>}{m<}{mn} minutes {m>}{s<}{sn} seconds{s>} ago'}); 
					});
					$('#snet_comment_field_' + status).val('');
					$('#snet_comment_errors_' + status).hide();
					$(".snet_comment").fadeIn().hover(function(){
						var element = $(this).attr("id");
						var comment = element.replace("comment","");
						$("#snet_delete_comment_" + comment).show();
					},function(){
						var element = $(this).attr("id");
						var comment = element.replace("comment","");
						$("#snet_delete_comment_" + comment).hide();
					});
					$('#snet_num_comments_' + status).html(parseInt($('#snet_num_comments_' + status).html())+1);
				}
			},
			error: function(){
				snet_ajax_indicator_hide();
			}				
		}).responseText;		
	}
	
	// No matter if we failed or not, we need to re-enable convo mode if we had it on before
	if(snet_convo_mode == 1)
	{
		$("#snet_enable_convo_mode").remove();
		$(".snet_comment_field").keydown(function(){ 
		 if (event.keyCode == '13')
		 {
			$(this).unbind('keydown').parent().parent('form').append('<input type="hidden" id="snet_enable_convo_mode" value="1" />').submit();
		}
		});
	}
	
	return false;
}

function snet_toggle_convo_mode(mode)
{
	if(mode == '1')
		var is_enabled = true;
	else if(mode == '0')
		var is_enabled = false;
		
	if(!is_enabled)
	{
		$('.snet_comment_submit').hide();
		$('.snet_comment_field').keydown(function(){ 
			if (event.keyCode == '13' && !event.shiftKey)
			{
				$(this).unbind('keydown').parent().parent('form').append('<input type="hidden" id="snet_enable_convo_mode" value="1" />').submit();
				alert('eneter');
			}
			else if(event.keyCode == '13' && event.shiftKey)
			{
				$(this).append('&#13;Newline');
				alert('newline');
			}
		});
		$('.snet_convo_mode_status').html('<strong>ON</strong>, hit enter to post');
		$('.snet_toggle_convo').attr('checked','checked').val('1');
	}
	else
	{
		$('.snet_comment_submit').show();
		$('#snet_enable_convo_mode').remove();
		$('.snet_comment_field').unbind('keydown');
		$('.snet_comment_field').unbind('keydown');
		$('.snet_convo_mode_status').html('<strong>OFF</strong>');
		$('.snet_toggle_convo').attr('checked','').val(0);
	}
	
	return false;
}

function snet_validate_status_update()
{

	$('#snet_status_update_submit').attr('disabled','disabled');
	
	var type = $('#snet_status_type_hidden').val();
	var profile = $('#snet_profile_id').val();
	
	// Since we're doing alternating backgrounds, we need to figure out which one is next.
	var alt = true;
	if($('.snet_status').children('div.windowbg2').length == 0)
		alt = false;
	else
		alt = true;

	if(type == 1)
	{
		var snet_message = Encoder.htmlEncode($('#snet_message_box_1').val());

		if(snet_message != '')
		{
			snet_ajax_indicator_show();
			$.ajax({
				url: smf_scripturl + '?action=snet-ajax;sa=poststatus',
				global: false,
				type: 'POST',
				data: ({snet_message : snet_message, profile : profile, altbg : alt, type : 1}),
				success: function(sreturn){
					snet_process_status_update_return(sreturn);
				},
				error: function(){
					snet_ajax_indicator_hide();
				}				
			}).responseText;	
			$('#snet_comment_field_' + status).val('');			
		}
		else
			$('#snet_status_update_submit').removeAttr('disabled');
	}
	else if(type == 2)
	{
		var webcam_data_string = $('#snet_webcam_data_string').val();
		var snet_image_link = $('#snet_image_link').val();
		var snet_caption = Encoder.htmlEncode($('#snet_message_box_2').val());
		var method = parseInt($('#snet_photo_method_hidden').val());
		
		if(method == 1)
			var data = ({webcam_data_string : webcam_data_string, snet_caption : snet_caption, profile : profile, altbg : alt, type : 2, photo_method : 1});
		else if(method == 2)
			return true;
		else if(method == 3)			
			var data = ({snet_image_link : snet_image_link, snet_caption : snet_caption, profile : profile, altbg : alt, type : 2, photo_method : 3});

		snet_ajax_indicator_show();
		$.ajax({
			url: smf_scripturl + '?action=snet-ajax;sa=poststatus',
			global: false,
			type: 'POST',
			data: data,
			success: function(sreturn){
				snet_process_status_update_return(sreturn);
			},
			error: function(){
				snet_ajax_indicator_hide();
			}				
		}).responseText;	
		$('#snet_comment_field_' + status).val('');			
	}
	else if(type == 3)
	{
		var snet_link = $('#snet_link').val();
		var snet_link_title = Encoder.htmlEncode($('#snet_link_title').val());
		var snet_link_comment = Encoder.htmlEncode($('#snet_message_box_3').val());
		if(snet_link != '')
		{
			snet_ajax_indicator_show();
			$.ajax({
				url: smf_scripturl + '?action=snet-ajax;sa=poststatus',
				global: false,
				type: 'POST',
				data: ({snet_link : snet_link, snet_link_title : snet_link_title, snet_link_comment : snet_link_comment, profile : profile, altbg : alt, type : 3}),
				success: function(sreturn){
					snet_process_status_update_return(sreturn);
				},
				error: function(){
					snet_ajax_indicator_hide();
				}				
			}).responseText;		
		}	
		else
			$('#snet_status_update_submit').removeAttr('disabled');		
	}	
	else if(type == 4)
	{
		var snet_quote = Encoder.htmlEncode($('#snet_quote').val());
		var snet_quote_source = Encoder.htmlEncode($('#snet_quote_source').val());
		var snet_quote_comment = Encoder.htmlEncode($('#snet_message_box_4').val());		
		if(snet_quote != '')
		{
			snet_ajax_indicator_show();
			$.ajax({
				url: smf_scripturl + '?action=snet-ajax;sa=poststatus',
				global: false,
				type: 'POST',
				data: ({snet_quote : snet_quote, snet_quote_source : snet_quote_source, snet_quote_comment : snet_quote_comment, profile : profile, altbg : alt, type : 4}),
				success: function(sreturn){
					snet_process_status_update_return(sreturn);
				},
				error: function(){
					snet_ajax_indicator_hide();
				}				
			}).responseText;		
		}
		else
			$('#snet_status_update_submit').removeAttr('disabled');		
	}	
	else if(type == 5)
	{
		var snet_video_link = $('#snet_video_link').val();
		var snet_video_comment = Encoder.htmlEncode($('#snet_message_box_5').val());
		if(snet_video_link != '')
		{
			snet_ajax_indicator_show();
			$.ajax({
				url: smf_scripturl + '?action=snet-ajax;sa=poststatus',
				global: false,
				type: 'POST',
				data: ({snet_video_link : snet_video_link, snet_video_comment : snet_video_comment, profile : profile, altbg : alt, type : 5}),
				success: function(sreturn){
					snet_process_status_update_return(sreturn);		
				},
				error: function(){
					snet_ajax_indicator_hide();
				}				
			}).responseText;		
		}	
		else
			$('#snet_status_update_submit').removeAttr('disabled');		
	}		
	return false;
}

function snet_like_comment(comment,profile)
{
	var num_likers = parseInt($('#snet_num_likers' + comment).html());
	var num_dislikers = parseInt($('#snet_num_dislikers' + comment).html());	
	
	snet_ajax_indicator_show();
	$.ajax({
		url: smf_scripturl + '?action=snet-ajax;sa=likecomment',
		global: false,
		type: 'POST',
		data: ({comment : comment, profile : profile}),
		success: function(sreturn){
			if($('#snet_num_likers' + comment).hasClass('snet_we_like'))
				$('#snet_num_likers' + comment).html(num_likers-1).removeClass('snet_we_like');
			else
				$('#snet_num_likers' + comment).html(num_likers+1).addClass('snet_we_like');
				
			if($('#snet_num_dislikers' + comment).hasClass('snet_we_like'))
				$('#snet_num_dislikers' + comment).html(num_dislikers-1).removeClass('snet_we_like');				
				
			snet_ajax_indicator_hide();
		},
		error: function(){
			snet_ajax_indicator_hide();
		}				
	}).responseText;		
	
	return false;
}

function snet_dislike_comment(comment,profile)
{
	var num_dislikers = parseInt($('#snet_num_dislikers' + comment).html());
	var num_likers = parseInt($('#snet_num_likers' + comment).html());
	
	snet_ajax_indicator_show();
	$.ajax({
		url: smf_scripturl + '?action=snet-ajax;sa=dislikecomment',
		global: false,
		type: 'POST',
		data: ({comment : comment, profile : profile}),
		success: function(sreturn){
			if($('#snet_num_dislikers' + comment).hasClass('snet_we_like'))
				$('#snet_num_dislikers' + comment).html(num_dislikers-1).removeClass('snet_we_like');
			else
				$('#snet_num_dislikers' + comment).html(num_dislikers+1).addClass('snet_we_like');
				
			if($('#snet_num_likers' + comment).hasClass('snet_we_like'))
				$('#snet_num_likers' + comment).html(num_likers-1).removeClass('snet_we_like');
				
			snet_ajax_indicator_hide();
		},
		error: function(){
			snet_ajax_indicator_hide();
		}				
	}).responseText;		
	
	return false;
}

function snet_view_all_comments(status,profile)
{
	var num_comments = parseInt($('#snet_num_comments_' + status).html());
	
	if(num_comments <= 20)
	{
		$('#comments' + status).children('.snet_comment').fadeOut();
		snet_ajax_indicator_show();
		$.ajax({
			url: smf_scripturl + '?action=snet-ajax;sa=loadcomments',
			global: false,
			type: 'POST',
			data: ({status : status, profile: profile}),
			success: function(creturn){
				$('#comments' + status).children('.snet_comment').fadeOut();
				$('#comments' + status).html(creturn);
				$('#snet_show_more_comments_link_' + status).hide();
				$('#snet_show_less_comments_link_' + status).show();
				$('#comments' + status).children('.snet_comment').fadeIn();
				snet_scroll_to('#snet_more_comments_' + status);
				$(".snet_comment").fadeIn().hover(function(){
					var element = $(this).attr("id");
					var comment = element.replace("comment","");
					$("#snet_delete_comment_" + comment).show();
				},function(){
					var element = $(this).attr("id");
					var comment = element.replace("comment","");
					$("#snet_delete_comment_" + comment).hide();
				});				
				if(parseInt($('#snet_num_comments_' + status).html()) <= 2)
					$('#snet_more_comments_' + status).slideUp();

				snet_ajax_indicator_hide();
			},
			error: function(){
				snet_ajax_indicator_hide();
				return true;
			}				
		}).responseText;
		
		return false;
	}
	else
		return true;
	
}

function snet_show_less(status)
{
	$('#comments' + status).children('.snet_comment').not('.snet_bottom_comment').slideUp(1000,function(){ $(this).remove(); });
	$('#snet_show_more_comments_link_' + status).show();
	$('#snet_show_less_comments_link_' + status).hide();
	return false;
}

function snet_view_older_comments(status,profile)
{
	var start = parseInt($('#snet_shown_comments').html());
	var num_comments = parseInt($('#snet_num_comments').html());

	$('#comments' + status).children('.snet_comment').fadeOut();
	snet_ajax_indicator_show();
	$.ajax({
		url: smf_scripturl + '?action=snet-ajax;sa=loadcomments',
		global: false,
		type: 'POST',
		data: ({status : status, profile: profile, start : start, limit : 10, single_view : true}),
		success: function(creturn){
			$('#snet_shown_comments').html(start + 25);
			if(start+25 >= num_comments)
				$('#snet_more_comments_' + status).hide();
			else
				$('#snet_shown_comments_display').html(start+25);
				
			$('#comments' + status).children('.snet_comment').fadeOut();
			$('#comments' + status).html(creturn);
			$('#comments' + status).children('.snet_comment').fadeIn();
			$(".snet_comment").fadeIn().hover(function(){
				var element = $(this).attr("id");
				var comment = element.replace("comment","");
				$("#snet_delete_comment_" + comment).show();
				},function(){
				var element = $(this).attr("id");
				var comment = element.replace("comment","");
				$("#snet_delete_comment_" + comment).hide();
			});				
			
			snet_ajax_indicator_hide();
		},
		error: function(){
			snet_ajax_indicator_hide();
			return true;
		}				
	}).responseText;
		
	return false;
}

function snet_comment_error(error,status)
{
	$('#snet_comment_submit_' + status).removeAttr('disabled');
	$('#snet_comment_errors_' + status).fadeIn();
	$('#snet_comment_errors_ul_' + status).html('').append('<li>' + error + '</li>');
}

function snet_post_error(error)
{
	$('#snet_status_update_submit').removeAttr('disabled');
	$('#snet_status_update_errors').fadeIn();
	$('#snet_status_update_errors_ul').html('').append('<li>' + error + '</li>');
}

function snet_ajax_indicator_show()
{
	if (typeof window.ajax_indicator == "function")
		ajax_indicator(true);

	$('#ajax_in_progress').css('display','none').slideDown();	
}

function snet_ajax_indicator_hide()
{
	$('#ajax_in_progress').slideUp(function(){},function(){ ajax_indicator(false); });
}

function snet_toggle_comment_form(status,scroll)
{
	$('#snet_status_comment_' + status).show();
	$('#snet_comment_field_' + status).focus();
	
	if(scroll)
		snet_scroll_to('#snet_status_comment_' + status);

	return false;
}

function snet_read_link()
{

	// Could this possibly be a link? We don't wanna poke the server if it ain't.
	var value = $('#snet_link').val();
	if((strpos(value,'http://') === false))
		value = 'http://' + value;
	
	var pattern = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i;
	alert(pattern.test(value));
	return false;
	// Send it to the server and have it look for all the stuff we need
	snet_ajax_indicator_show();
	$.ajax({
		url: smf_scripturl + '?action=snet-ajax;sa=likestatus',
		global: false,
		type: 'POST',
		data: ({status : status, profile : profile}),
		success: function(lreturn){
			if(lreturn != '0')
			{
				$('#snet_likers_' + status).html(lreturn);	
				$('#snet_likers_container_' + status).show();
			}			
			else
			{
				$('#snet_likers_container_' + status).hide();	
			}
			if($('#snet_like_button_' + status).children('a').html() == 'Like')
			{
				$('#snet_like_button_' + status).children('a').html('Unlike');
				$('#snet_dislike_button_' + status).hide().children('a').html('Dislike');
			}
			else
			{
				$('#snet_like_button_' + status).children('a').html('Like');
				$('#snet_dislike_button_' + status).show().children('a').html('Dislike');	
			}
			snet_ajax_indicator_hide();
		},
		error: function(){
			snet_ajax_indicator_hide();
		},
		dataType: 'json',		
	}).responseText;		
}

function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another  
    // 
    // version: 1101.3117
    // discuss at: http://phpjs.org/functions/strpos
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
    var i = (haystack+'').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}

function snet_process_status_update_return(sreturn)
{
	snet_ajax_indicator_hide();
					
	// Is it an error message? Oh my, oh my.
	if((strpos(sreturn,'snet_error_') !== false))
	{
		sreturn = sreturn.replace('snet_error_','');
		snet_post_error(sreturn);
	}
	else
	{
		// It's posted. Display it to the user so the lazy peep doesn't have to reload the window.
		$('#snet_status_updates').prepend(sreturn);
		$('.snet_date').each(function(){
		var date = $(this).attr('id');
		var date = parseInt(date.replace('d_','')) * 1000;
		var date = new Date(date);
		var last_date = new Date()-3;
		if(date<last_date)
			$(this).countdown({since: date, significant: 1, layout: '{d<}{dn} days {d>}{h<}{hn} hours {h>}{m<}{mn} minutes {m>}{s<}{sn} seconds{s>} ago'}); 
		});
		$('#snet_status_update_errors').hide();
		$('#snet_status_update_submit').removeAttr('disabled');
		$('#snet_no_status_updates').hide();
		$('textarea').elastic();
		$('.snet_status').fadeIn().hover(function(){
			var element = $(this).attr("id");
			var status = element.replace("status","");
			$("#snet_delete_" + status).show();
		},function(){
			var element = $(this).attr("id");
			var status = element.replace("status","");
			$("#snet_delete_" + status).hide();
		});
		snet_scroll_to('.snet_status');
		
		// Empty out the fields. Could probably be done a better way but meh.
		var status_type = $('#snet_status_type_hidden').val();
		var profile_id = $('#snet_profile_id').val();
		var session = $('#snet_session').val();		
		var submit_button = $('#snet_status_update_submit').val();		
		$(':input','#snet_status_update').val('');
		$('#snet_status_type_hidden').val(status_type);
		$('#snet_profile_id').val(profile_id);
		$('#snet_session').val(session);		
		$('#snet_status_update_submit').val(submit_button);			
	}
}

function snet_like_status_update(status,profile)
{
	snet_ajax_indicator_show();
	$.ajax({
		url: smf_scripturl + '?action=snet-ajax;sa=likestatus',
		global: false,
		type: 'POST',
		data: ({status : status, profile : profile}),
		success: function(lreturn){
			snet_scroll_to('#status' + status);
			if(lreturn != '0')
			{
				$('#snet_likers_' + status).html(lreturn);	
				$('#snet_likers_container_' + status).show();
			}			
			else
			{
				$('#snet_likers_container_' + status).hide();	
			}
			if($('#snet_like_button_' + status).children('a').html() == 'Like')
			{
				$('#snet_like_button_' + status).children('a').html('Unlike');
				$('#snet_dislike_button_' + status).hide().children('a').html('Dislike');
				snet_toggle_comment_form(status);
			}
			else
			{
				$('#snet_like_button_' + status).children('a').html('Like');
				$('#snet_dislike_button_' + status).show().children('a').html('Dislike');	
			}
			snet_ajax_indicator_hide();
		},
		error: function(){
			snet_ajax_indicator_hide();
		},
		dataType: 'json',		
	}).responseText;		
	
	return false;
}

function snet_dislike_status_update(status,profile)
{
	snet_ajax_indicator_show();
	$.ajax({
		url: smf_scripturl + '?action=snet-ajax;sa=dislikestatus',
		global: false,
		type: 'POST',
		data: ({status : status, profile : profile}),
		success: function(lreturn){
			snet_scroll_to('#status' + status);
			if(lreturn != '0')
			{
				$('#snet_dislikers_' + status).html(lreturn);	
				$('#snet_dislikers_container_' + status).show();
			}
			else
			{
				$('#snet_dislikers_container_' + status).hide();	
			}
			if($('#snet_dislike_button_' + status).children('a').html() == 'Dislike')
			{
				$('#snet_dislike_button_' + status).children('a').html('Un-dislike');
				$('#snet_like_button_' + status).hide().children('a').html('Like');
				snet_toggle_comment_form(status,false);
			}
			else
			{
				$('#snet_dislike_button_' + status).children('a').html('Dislike');
				$('#snet_like_button_' + status).show().children('a').html('Like');	
			}			
			snet_ajax_indicator_hide();
		},
		error: function(){
			snet_ajax_indicator_hide();
		}				
	}).responseText;		
	
	return false;
}

// From the jqery.blink plugin, used for the "new version" warning in the ACP.
(function($)
{
	$.fn.blink = function(options)
	{
		var defaults = { delay:500 };
		var options = $.extend(defaults, options);
		
		return this.each(function()
		{
			var obj = $(this);
			setInterval(function()
			{
				if($(obj).css("visibility") == "visible")
				{
					$(obj).css('visibility','hidden');
				}
				else
				{
					$(obj).css('visibility','visible');
				}
			}, options.delay);
		});
	}
}(jQuery))